# -*- coding: utf-8 -*-
# @Time : 2023/7/3 13:08:35
# @Author : sacon
# @File : MonitoringOperation.py

import requests,uuid
import time
from loguru import logger


class MonitoringOperation:
    def __init__(self, task_id):
        """
        监控定时任务
        """
        self.task_id = task_id


    def request_handle(self, procedure_id, run_id,Type='request_before'):
        if Type != 'request_before' and Type != 'request_after':
            logger.exception(f'Type参数错误，只能为request_before或者request_after')
            return

        data = {
            'procedure_id': procedure_id,  # 流程id，通过上面的获取所有流程接口获取
            'run_id': run_id  # 运行id，这个是自己生成的，用于后面的请求
        }
        url = f'http://monitoring.dic.cool:9001/{Type}/'
        try:
            response = requests.post(url, json=data)
        except Exception as e:
            logger.error("定时任务监控接口访问失败！")

    def __call__(self, fun):
        def retry_handle(*args, **kwargs):
            run_id = str(uuid.uuid4())
            self.request_handle(self.task_id, run_id, Type='request_before')
            fun_res = fun(*args, **kwargs)
            self.request_handle(self.task_id, run_id, Type='request_after')
            return fun_res

        return retry_handle


@MonitoringOperation(2)
def aaa():
    print('test')
    time.sleep(5)
    return 'test'

if __name__ == '__main__':
    aaa()